#include "Data.h"

Data::Data() {

	_currentTaskID = 0;
	
}

Data::~Data() {

}

Data::DataFeedback Data::executeAdd( Task taskToAdd ) {
	
	_currentTaskID++;
	taskToAdd.setTaskID(_currentTaskID);
	
	_list.push_back(taskToAdd);
	return SUCCESS;
	
}

Data::DataFeedback Data::executeDeleteTaskID( int taskID ) {

	int check = 0;

	if( _list.empty() ) {

		return FAILURE;

	}

	for(unsigned int i = 0 ; i < _list.size() ; i++ ) {

		if( _list.at(i).getTaskID() == taskID ) {

			for(unsigned int j = i ; j < _list.size() - 1 ; j ++ ) {
			
				_list[j] = _list[j+1] ;
			
			}

			_list.pop_back();
			check = 1;

		}

	}

	if( check == 1 ) {

		return SUCCESS;
	
	}

	else {
		
		return FAILURE;

	}

}

Data::DataFeedback Data::executeClear() {

	_list.clear();

	_currentTaskID = 0;

	if( _list.size() == 0 ) {
	
		return SUCCESS;

	}

	else {
		
		return FAILURE;

	}

}

Data::DataFeedback Data::executeEdit( Task taskToEdit ) {

	if( _list.empty() ) {

		return FAILURE;

	}

	else {

		for( int i = 0 ; i < _list.size() ; i ++ ) {

			if( _list.at(i).getTaskID() == taskToEdit.getTaskID() ) {

				_list.at(i) = taskToEdit;

			}

		}

		return SUCCESS;

	}
	
}

Data::DataFeedback Data::getAll( vector<Task>& tasks ) {

	if( _list.empty() ) {

		return FAILURE;
	
	}

	else {

		for( unsigned int i = 0 ; i < _list.size() ; i++ ) {

			tasks.push_back( _list.at(i) );
	
		}
	
	return SUCCESS;

	}

}

Data::DataFeedback Data::getByName( vector<Task>& tasks, string taskName ) {

	tasks.clear();

	for( int i = 0 ; i < _list.size() ; i ++ ) {

		if( _list.at(i).getTaskName() == taskName ) {
			
			tasks.push_back( _list.at(i) );

		}

	}

	if( tasks.empty() ) {
	
		return FAILURE;
	
	}
	
	else {
	
		return SUCCESS;
	
	}

}

Data::DataFeedback Data::getByID( vector<Task>& tasks, int taskId ) {

	tasks.clear();

	for( int i = 0 ; i < _list.size() ; i ++ ) {

		if( _list.at(i).getTaskID() == taskId ) {
			
			tasks.push_back( _list.at(i) );

		}

	}

	if( tasks.empty() ) {
	
		return FAILURE;
	
	}
	
	else {
	
		return SUCCESS;
	
	}
		
}

Data::DataFeedback Data::getBySearchTerm ( vector<Task>& tasks, string searchTerm ) { 

	tasks.clear();
	
	for( int i = 0 ; i < _list.size() ; i ++ ) {

		if( _list.at(i).getTaskName() == searchTerm ) {
			
			tasks.push_back( _list.at(i) );

		}

		else {
		
			char* csearchTerm = new char[searchTerm.size()+1];
			const char* check;

			strcpy( csearchTerm , searchTerm.c_str() );
			check = strstr( _list.at(i).getTaskName().c_str() , csearchTerm );
			
			if( check == NULL ) {
				
				continue;

			}

			else {

				tasks.push_back( _list.at(i) );

			}

		}

	}


	if( tasks.empty() ) {
	
		return FAILURE;
	
	}
	
	else {
	
		return SUCCESS;
	
	}
	
}

Data::DataFeedback Data::getByTime (  vector<Task>& tasks, ptime startTime, ptime endTime ) {

	return SUCCESS;
	/*tasks.clear();

	for( unsigned int i = 0 ; i < _list.size() ; i ++ ) {

		if( _list.at(i).getStartTime() >= startTime && _list.at(i).getEndTime() <= endTime ) {

			tasks.push_back( _list.at(i) );
			
		}

	}

	if( tasks.empty() ) {
	
		return FAILURE;
	
	}
	
	else {
	
		return SUCCESS;
	
	}
	*/
}

Data::DataFeedback Data::getByDone( vector<Task>& tasks ) {

	tasks.clear();

	for( int i = 0 ; i < _list.size() ; i ++ ) {

		if( _list.at(i).getDone() == true ) {
			
			tasks.push_back( _list.at(i) );

		}

	}

	if( tasks.empty() ) {
	
		return FAILURE;
	
	}
	
	else {
	
		return SUCCESS;
	
	}
		
}

Data::DataFeedback Data::executeLoad( vector<Task> list ) {

	_list.clear();

	if( list.empty() ) { 

		return SUCCESS;

	}
	
	else {

	for(int i = 0 ; i < list.size() ; i ++ ) {

		_list.push_back( list.at(i) );
	
	}

	_currentTaskID = highestTaskID( list );

	return SUCCESS;
	
	}

}

int Data::getCurrentTaskID() {

	return _currentTaskID;

}

int Data::getCapacity() {

	return _list.size();

}

int Data::highestTaskID( vector<Task> tasks ) {

	int max = 0;

	for ( int i = 0 ; i < tasks.size() ; i ++ ) {

		if( max < tasks.at(i).getTaskID() ) {

			max = tasks.at(i).getTaskID();

		}

	}

	return max;

}